package net.mudfox.stage;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import net.mudfox.base.BaseRestActivity;
import net.mudfox.factory.Entry;
import net.mudfox.factory.ID;
import net.mudfox.factory.Url;
import net.mudfox.helper.AdapterHelper;
import net.mudfox.helper.CommonHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends BaseRestActivity {
    ListView listView;
    SwipeRefreshLayout swipeLayout;
    JSONArray array;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actionBar.setDisplayHomeAsUpEnabled(false);

        listView = findViewById(R.id.listView);
        swipeLayout = findViewById(R.id.swipeLayout);
        registerForContextMenu(listView);

        entry = Entry.PurchaseLog;
        baseUrl = Url.PurchaseLog;

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, ID.ACTION_ADD, 0, "添加...");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        // getMenuInflater().inflate(R.menu.operate_menu, menu);
        menu.add(0, ID.ACTION_COPY, 0, "复制");
        menu.add(0, ID.ACTION_EDIT, 1, "编辑...");
        menu.add(0, ID.ACTION_REMOVE, 2, "删除");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (array.isEmpty())
            return false;

        return super.onContextItemSelected(item);
    }

    boolean do_copyItem(JSONObject object) {
        String detail = object.getString("detail");
        if (detail.isEmpty())
            return false;

        CommonHelper.setClipboardText(this, entry, detail);
        CommonHelper.showMessage(this, "已复制到剪贴板");
        return true;
    }

    boolean do_removeItem(JSONObject object) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确定操作").setMessage("确定删除吗？");
        builder.setPositiveButton(R.string.sure,
                (paramAnonymousDialogInterface, paramAnonymousInt) ->
                {
                    int id = object.getIntValue("id");
                    if (id > 0)
                        httpGet(baseUrl + "_remove/" + id);
                });

        builder.setNegativeButton(R.string.cancel,
                (paramAnonymousDialogInterface, paramAnonymousInt) -> {
                });

        builder.create().show();
        return true;
    }

    @Override
    public void processReply(JSONObject rootObject) {
        array = AdapterHelper.verifyObject(rootObject, entry);
        if (array.isEmpty()) {
            listView.setAdapter(null);
            return;
        }

        List<Map<String, Object>> model = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = array.getJSONObject(i);
            Map<String, Object> item = new HashMap<>();
            item.put("date_", object.getString("date_"));
            String detail = object.getString("label") + "  " +
                    object.getString("model") + "  " +
                    object.getDouble("number");
            item.put("detail", detail);
            model.add(item);
        }

        SimpleAdapter adapter = AdapterHelper.getTableAdapter(this, model,
                new String[]{"date_", "detail"});
        listView.setAdapter(adapter);
    }
}
